<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>withResolvers</title>
  <style>
    *,
    *::after,
    *::before {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    :root {
      min-height: 100vh;
    }

    :root:focus-within {
      scroll-behavior: smooth;
    }

    body {
      min-height: inherit;
    }
  </style>
</head>

<body>
  <div></div>
</body>
<script>
  "use strict"; {
    const promise = new Promise((resolve, reject) => {
      setTimeout(() => {
        let ok = false
        if (ok) {
          resolve("操作成功了")
        } else {
          reject(new Error("失败了"))
        }
      }, 1 * 1000)
    })

    promise.then(res => {
      console.log(res)
    }).catch(err => {
      console.error(err)
    })
  }; {
    // withResolvers 减少了嵌套的使用了
    const { promise, resolve, reject } = Promise.withResolvers()
    setTimeout(() => {
      let ok = true
      if (ok) {
        resolve("操作成功了")
      } else {
        reject(new Error("操作失败了"))
      }
    }, 1 * 100)

    promise.then(res => {
      console.log("res ==", res);
    }).catch(err => {
      console.log(err);
    })
  };
</script>

</html>